package com.kedao.common;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.kedao.interceptor.SessionInterceptor;
import com.kedao.model.User;
import com.kedao.routes.AdminRoutes;
import com.kedao.routes.FrontRoutes;
import com.kedao.util.PropertiesContent;

/**
 * 
 * 2013年12月10日 10:23:40
 * 
 * @author anhuifeng
 * 
 */
public class SystemConfig extends JFinalConfig {

	private boolean isLocal = isDevMode();

	/**
	 * 配置常量
	 */
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		if (isLocal) {
			me.setDevMode(true);
		}
		me.setError404View("/assets/common/404.html");
		me.setError500View("/assets/common/500.html");
		// me.setBaseViewPath("/WEB-INF/pages/");
	}

	/**
	 * 配置路由（手动配置）
	 */
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		// 前端路由
		me.add(new FrontRoutes());
		// 后端路由
		me.add(new AdminRoutes());
	}

	/**
	 * 配置处理器
	 */
	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub

	}

	/**
	 * 配置全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.add(new SessionInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		// 配置数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(PropertiesContent
				.get("jdbc.url"), PropertiesContent.get("jdbc.username"),
				PropertiesContent.get("jdbc.password"), PropertiesContent
						.get("jdbc.driver"));
		// WallFilter wall = new WallFilter();
		// wall.setDbType(PropertiesContent.get("jdbc.dbType"));
		// druidPlugin.addFilter(wall);
		druidPlugin.addFilter(new StatFilter());
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);
		// 数据库表与bean映射
		arp.addMapping("kd_user", User.class);

		if (PropertiesContent.get("jdbc.driver").equals(
				"net.sourceforge.jtds.jdbc.Driver")) {
			arp.setDialect(new AnsiSqlDialect());
		}

		arp.setShowSql(true);

		// 启用缓存
		// me.add(new EhCachePlugin());
	}

	private boolean isDevMode() {
		String osName = System.getProperty("os.name");
		return osName.indexOf("Windows") != -1;
	}

}
